home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / wdj0796.zip / BENCHMAR.ZIP / MEMBENCH.CPP next >
C/C++ Source or Header  |  1996-04-12  |  3KB  |  120 lines

  1. #include <assert.h>
  2. #include <windows.h>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. #define CPS ((int)CLOCKS_PER_SEC)
  8.  
  9. long NIterations;
  10. int NBlocks;        /* total number of memory blocks to allocate    */
  11. int MinBlockSize;   /* minimum block size to allocate               */
  12. int MaxBlockSize;   /* maximum block size to allocate               */
  13.  
  14. int     DummyCount; // so no one can optimize my dummies away
  15. void    DummyFree(void* Foo);
  16. void*   DummyMalloc(size_t Size);
  17.  
  18. void    BenchMark(void)
  19.     {
  20.     clock_t     StartTime, StopTime, LoopTime, TotalTime;
  21.     long    i;
  22.     void**  Pointers;
  23.     int*    Sizes;
  24.     /* first, create array of pointers */
  25.     Pointers    = (void**)calloc(NBlocks, sizeof(void*));
  26.     Sizes       = (int*)calloc(NBlocks, sizeof(int));
  27.  
  28.     /* second, initialize sizes to random values */
  29.     for(i=0; i < NBlocks; ++i)
  30.         {
  31.         int Size = rand() % ((MaxBlockSize-MinBlockSize) + 1);
  32.         Size    += MinBlockSize;
  33.         Sizes[i]    = Size;
  34.         }
  35.  
  36.     /* third, measure basic loop time */
  37.     StartTime   = clock();
  38.     for(i = 0; i < NIterations; ++i)
  39.         {
  40.         int iBlock  = i % NBlocks;
  41.         if(Sizes[iBlock]&1) // randomly call dummy free or malloc
  42.             {
  43.             DummyFree(Pointers[iBlock]);
  44.             Pointers[iBlock]    = 0;
  45.             }
  46.         else
  47.             Pointers[iBlock]    = DummyMalloc(Sizes[iBlock]);
  48.         }
  49.  
  50.     StopTime    = clock();
  51.     LoopTime    = StopTime - StartTime;
  52.  
  53.     /* fourth, allocate a random set of blocks */
  54.     for(i=0; i < NBlocks; ++i)
  55.         if(rand() & 1)
  56.             Pointers[i]    = new char[Sizes[i]];
  57.  
  58.  
  59.     /* finally, perform benchmark */
  60.     StartTime   = clock();
  61.     for(i = 0; i < NIterations; ++i)
  62.         {
  63.         int iBlock  = i % NBlocks;
  64.         if(Pointers[iBlock])
  65.             {
  66.             delete[] Pointers[iBlock];
  67.             Pointers[iBlock]    = 0;
  68.             }
  69.         else
  70.             Pointers[iBlock]    = new char[Sizes[iBlock]];
  71.         }
  72.     StopTime    = clock();
  73.     TotalTime   = (StopTime - StartTime) - LoopTime;
  74.  
  75.     printf("NIterations=%ld, NBlocks=%d, MinBlockSize=%d, MaxBlockSize=%d\n",
  76.         NIterations, NBlocks, MinBlockSize, MaxBlockSize);
  77.     {
  78.     int Secs = TotalTime / CPS;
  79.     int Decs = (TotalTime-(Secs*CPS)) / (CPS/10);
  80.  
  81.     printf( "Time = %d.%d\n", Secs, Decs);
  82.     printf( "memory time=%ld, loop time=%ld\n", TotalTime, LoopTime);
  83.     }
  84.     }
  85.  
  86. void    main(int argc, char**argv)
  87.     {
  88.     // assume at least hundredths of a second resolution
  89.     assert(CPS >= 100);
  90.  
  91.     MinBlockSize    = 4;
  92.     NIterations     = 1000L*1000L*5;
  93.     if(argc == 3)
  94.         {
  95.         NBlocks         = atoi(argv[1]);
  96.         MaxBlockSize    = atoi(argv[2]);
  97.         }
  98.     if(NBlocks < 1 || MaxBlockSize < MinBlockSize)
  99.         fprintf(stderr, "Usage: membench <#of blocks> <max block size>\n");
  100.     else
  101.         BenchMark();
  102.     exit(DummyCount?EXIT_SUCCESS:EXIT_FAILURE);
  103.     }
  104.  
  105. // the runtime library will be compared against these
  106. // dummy functions, which represent absolute fastest
  107. // that memory management could be (they do nothing)
  108. void    DummyFree(void* /*Foo*/)
  109.     {
  110.     ++DummyCount;
  111.     }
  112.  
  113. void*   DummyMalloc(size_t /*Size*/)
  114.     {
  115.     ++DummyCount;
  116.     return 0;
  117.     }
  118.  
  119.  
  120.